<?php
namespace Controller;

class Net extends Controller {
	private $MNet = NULL;

	public function __construct() {
		$this->MNet = new \Model\Net();
	}

	public function index() {
		$this->display();
	}

	public function info() {
		print_r($this->MNet->info());
	}

	/**
	 * 断网并重联，常用于换IP。
	 *
	 * @param int $_GET['connect']
	 * @param string $_GET['src']
	 * @return Location|string
	 */
	public function reconnect() {
		if (!isset($_GET['connect'])) {
			if ($this->MNet->disconnect()) {
				sleep(2);
				
				header('Location: /net/reconnect?connect=1' . (isset($_GET['src']) ? ('&src=' . urlencode($_GET['src'])) : ''));
			} else {
				echo "Router Can't control";
			}
			
			die();
		}
		
		$_GET['connect'] && $this->MNet->connect();
		
		$info = $this->MNet->info();
		if ($info['ip'] != '0.0.0.0') {
			if (isset($_GET['src'])) {
				header('Location: ' . $_GET['src']);
			} else {
				echo $info['ip'];
			}
			
			die();
		}
		
		sleep(1);
		
		header('Location: /net/reconnect?connect=0' . (isset($_GET['src']) ? ('&src=' . urlencode($_GET['src'])) : ''));
	}
}